package com.share.hope.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.share.hope.entity.UserCollectPoorChild;
import com.share.hope.entity.result.R;
import com.share.hope.mapper.PoorChildCollectMapper;
import com.share.hope.service.PoorChildCollectService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class PoorChildCollectServiceImpl extends ServiceImpl<PoorChildCollectMapper, UserCollectPoorChild> implements PoorChildCollectService {

    @Override
    public R<String> createCollect(Long userId, Long childId) {
        try {
            this.save(UserCollectPoorChild.builder().userId(userId).childId(childId).build());
            return R.ok("收藏成功");
        }catch (Exception ex) {
            log.error("[createCollect] create collect poor_child fail, error msg is: {}", ex.getMessage());
            return R.error("收藏失败或已收藏");
        }
    }

    @Override
    public R<String> cancelCollect(Long userId, Long childId) {
        LambdaQueryWrapper<UserCollectPoorChild> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserCollectPoorChild::getUserId, userId);
        wrapper.eq(UserCollectPoorChild::getChildId, childId);
        if (this.remove(wrapper)) {
            return R.ok("取消收藏成功");
        }
        return R.error("取消收藏失败");
    }
}
